import * as AES from 'crypto-js/aes'
import * as Base64 from 'crypto-js/enc-base64'
import * as EncUTF8 from 'crypto-js/enc-utf8'
import * as MD5 from 'crypto-js/md5'
import * as ECB from 'crypto-js/mode-ecb'
import * as Pkcs7 from 'crypto-js/pad-pkcs7'
// import CryptoJS from 'crypto-js';

const wordArray = EncUTF8.parse('wmHyIsYLExWcqhKcmo5To2ZY#UGE1g$S') // 十六位十六进制数作为密钥
const iv = EncUTF8.parse('ABCDEF1234123412') // 十六位十六进制数作为密钥偏移量

// 解密方法
export function encrypt(word: string): string {
  const encodeWord = EncUTF8.parse(word)
  const decryptHex = AES.encrypt(encodeWord, wordArray, { mode: ECB, padding: Pkcs7 })
  return decryptHex.toString()
}

// 加密方法
export function decrypt(word: string): string {
  const decryptHex = AES.decrypt(word, wordArray, { iv, mode: ECB, padding: Pkcs7 })
  return EncUTF8.stringify(decryptHex).toString()
}

export function encodeBase64(word: string): string {
  const encryptedHexStr = EncUTF8.parse(word)
  return Base64.stringify(encryptedHexStr)
}

export function decodeBase64(base64: string): string {
  const words = Base64.parse(base64)
  return words.toString(EncUTF8)
}

// 加密方法
export function md5(word: string): string {
  return MD5(word).toString()
}

// 生成指定格式的密钥字符串: WordArray
function _generateWordArray(length = 32): string {
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789#$'
  let result = ''
  for (let i = 0; i < length; i++) {
    result += chars.charAt(Math.floor(Math.random() * chars.length))
  }
  return result
}

// 生成指定格式的密钥字符串:IV
function _generateIv(length = 16): string {
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
  let result = ''
  for (let i = 0; i < length; i++) {
    result += chars.charAt(Math.floor(Math.random() * chars.length))
  }
  return result
}

// const enstr = encrypt('qwe123123')
// console.log(enstr)
// console.log(decrypt(enstr))
// console.info(`md5('qwe123123')`)
// console.info(md5('qwe123123'))
//
// export default {
//   decrypt,
//   encrypt,
//   decodeBase64,
//   encodeBase64,
//   md5,
// }
